/**************************************************************************//**
 * @file           main.c
 * @version        V1.0
 * $Revision:      1 $
 * $Date:          2017/01/04 $
 * @brief          Main function
 * @Author         Jeffrey Kang
 * @ContactInfo    chkang@esmt.com.tw
 *
 * @note
 *
 * Copyright (C) 1998 Elite Semiconductor Memory Technology Corp.
 * All rights reserved.
 *
 ******************************************************************************/


#include <REG52.h>
#include "system_FE81001.h"


#define FRAME_SEG_SIZE 14


/**
 * @brief       delay function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void delay (int cnt )
{
	unsigned int i , j ;
	
	for(j=0 ; j<cnt ; j++)
	for( i = 0 ; i < 200 ; i++ ); 
	
}


/**
 * @brief       LCMInit function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void LCMInit(void)
{
	unsigned char i;
	
	/* LCD clock enable */
	En_LCM_clk();
	
	/* LCD control register setting */
	LCM->control0 = 0x41;				// 0, ENLCDBUF = 1, freq = 0 (60Hz) 1(120Hz), LEVEL = 1(1/3 bias) 1(1/2 bias), VCTR = 0001
	
	/* Reset Frame Buffer */
	for( i = 6 ; i < FRAME_SEG_SIZE ; i++ ){
		LCM->frambuf[i] = 0x00;
	}
}
/**
 * @brief       IOCtrl function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void IOCtrl( void )
{
	/* enable GPIO clock */
	PORT_PAPBPCPD_Clock_Enable();
	
	/* enable PD input mode */
	PORT_SetPDInput( BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5 | BIT6 | BIT7 );
	
	/* enable PC input mode */
	PORT_SetPCInput(BIT0 | BIT1);
	
	/* enable PB input mode */
	PORT_SetPBInput(BIT6 | BIT7);
	
	/* Set PD4 as COM3 */
	Set_PD4_COM3();
	
	/* Set PD5 as COM2 */
	Set_PD5_COM2();
	
	/* Set PD6 as COM1 */
	Set_PD6_COM1();
	
	/* Set PD7 as COM0 */
	Set_PD7_COM0();

	/* Set PD0 as SEG03 */
	Set_PD0_SEG03();
	
	/* Set PD1 as SEG02 */
	Set_PD1_SEG02();
	
	/* Set PD2 as SEG01 */
	Set_PD2_SEG01();
	
	/* Set PD3 as SEG00 */
	Set_PD3_SEG00();
	
	/* Set PC0 as SEG11 */
	Set_PC0_SEG11();
	
	/* Set PC1 as SEG10 */
	Set_PC1_SEG10();
	
	/* Set PB6 as SEG13 */
	Set_PB6_SEG13();
	
	/* Set PB7 as SEG12 */
	Set_PB7_SEG12();
	
}

void LCM_Config()
{
	IOCtrl();
	
	LCMInit();
	
	LCM_on();
}

void Dig1( unsigned char num)
{
	switch(num)
	{
		
	    	case 0:
	    			LCM->frambuf[0]  = 0x0B;
	    			LCM->frambuf[1]  = 0x0E;
	    			break ;
	    	
	    	case 1:
	    			LCM->frambuf[0]  = 0x00;
	    			LCM->frambuf[1]  = 0x06;
	    		break ;
	    	
	    	case 2:
	    			LCM->frambuf[0]  = 0x07;
	    			LCM->frambuf[1]  = 0x0C;
	    		break ;
	    	
	    	case 3:
	    			LCM->frambuf[0]  = 0x05;
	    			LCM->frambuf[1]  = 0x0E;
	    		break ;
	    	
	    	case 4:
	    			LCM->frambuf[0]  = 0x0C;
	    			LCM->frambuf[1]  = 0x06;
	    		break ;
	    	
	    	case 5:
	    			LCM->frambuf[0]  = 0x0D;
	    			LCM->frambuf[1]  = 0x0A;
	    		break ;
	    	
	    	case 6:
	    			LCM->frambuf[0]  = 0x0F;
	    			LCM->frambuf[1]  = 0x02;
	    		break ;
	    	
	    	case 7:
	    			LCM->frambuf[0]  = 0x00;
	    			LCM->frambuf[1]  = 0x0E;
	    		break ;
	    	
	    	case 8:
	    			LCM->frambuf[0]  = 0x0F;
	    			LCM->frambuf[1]  = 0x0E;
	    		break ;
	    	
	    	case 9:
	    			LCM->frambuf[0]  = 0x0C;
	    			LCM->frambuf[1]  = 0x0E;
	    		break ;
		
	}
	
}
void Dig2( unsigned char num)
{
	switch(num)
	{
		
	    	case 0:
	    			LCM->frambuf[2]  = 0x0B;
	    			LCM->frambuf[3]  = 0x0E;
	    			break ;
	    	
	    	case 1:
	    			LCM->frambuf[2]  = 0x00;
	    			LCM->frambuf[3]  = 0x06;
	    		break ;
	    	
	    	case 2:
	    			LCM->frambuf[2]  = 0x07;
	    			LCM->frambuf[3]  = 0x0C;
	    		break ;
	    	
	    	case 3:
	    			LCM->frambuf[2]  = 0x05;
	    			LCM->frambuf[3]  = 0x0E;
	    		break ;
	    	
	    	case 4:
	    			LCM->frambuf[2]  = 0x0C;
	    			LCM->frambuf[3]  = 0x06;
	    		break ;
	    	
	    	case 5:
	    			LCM->frambuf[2]  = 0x0D;
	    			LCM->frambuf[3]  = 0x0A;
	    		break ;
	    	
	    	case 6:
	    			LCM->frambuf[2]  = 0x0F;
	    			LCM->frambuf[3]  = 0x02;
	    		break ;
	    	
	    	case 7:
	    			LCM->frambuf[2]  = 0x00;
	    			LCM->frambuf[3]  = 0x0E;
	    		break ;
	    	
	    	case 8:
	    			LCM->frambuf[2]  = 0x0F;
	    			LCM->frambuf[3]  = 0x0E;
	    		break ;
	    	
	    	case 9:
	    			LCM->frambuf[2]  = 0x0C;
	    			LCM->frambuf[3]  = 0x0E;
	    		break ;
		
	}
	
}
void Dig3( unsigned char num)
{
	switch(num)
	{
		
	    	case 0:
	    			LCM->frambuf[10]  = 0x0B;
	    			LCM->frambuf[11]  = 0x0E;
	    			break ;
	    	
	    	case 1:
	    			LCM->frambuf[10]  = 0x00;
	    			LCM->frambuf[11]  = 0x06;
	    		break ;
	    	
	    	case 2:
	    			LCM->frambuf[10]  = 0x07;
	    			LCM->frambuf[11]  = 0x0C;
	    		break ;
	    	
	    	case 3:
	    			LCM->frambuf[10]  = 0x05;
	    			LCM->frambuf[11]  = 0x0E;
	    		break ;
	    	
	    	case 4:
	    			LCM->frambuf[10]  = 0x0C;
	    			LCM->frambuf[11]  = 0x06;
	    		break ;
	    	
	    	case 5:
	    			LCM->frambuf[10]  = 0x0D;
	    			LCM->frambuf[11]  = 0x0A;
	    		break ;
	    	
	    	case 6:
	    			LCM->frambuf[10]  = 0x0F;
	    			LCM->frambuf[11]  = 0x02;
	    		break ;
	    	
	    	case 7:
	    			LCM->frambuf[10]  = 0x00;
	    			LCM->frambuf[11]  = 0x0E;
	    		break ;
	    	
	    	case 8:
	    			LCM->frambuf[10]  = 0x0F;
	    			LCM->frambuf[11]  = 0x0E;
	    		break ;
	    	
	    	case 9:
	    			LCM->frambuf[10]  = 0x0C;
	    			LCM->frambuf[11]  = 0x0E;
	    		break ;
		
	}
}
void Dig4( unsigned char num)
{
	switch(num)
	{
		
	    	case 0:
	    			LCM->frambuf[12]  = 0x0B;
	    			LCM->frambuf[13]  = 0x0E;
	    			break ;
	    	
	    	case 1:
	    			LCM->frambuf[12]  = 0x00;
	    			LCM->frambuf[13]  = 0x06;
	    		break ;
	    	
	    	case 2:
	    			LCM->frambuf[12]  = 0x07;
	    			LCM->frambuf[13]  = 0x0C;
	    		break ;
	    	
	    	case 3:
	    			LCM->frambuf[12]  = 0x05;
	    			LCM->frambuf[13]  = 0x0E;
	    		break ;
	    	
	    	case 4:
	    			LCM->frambuf[12]  = 0x0C;
	    			LCM->frambuf[13]  = 0x06;
	    		break ;
	    	
	    	case 5:
	    			LCM->frambuf[12]  = 0x0D;
	    			LCM->frambuf[13]  = 0x0A;
	    		break ;
	    	
	    	case 6:
	    			LCM->frambuf[12]  = 0x0F;
	    			LCM->frambuf[13]  = 0x02;
	    		break ;
	    	
	    	case 7:
	    			LCM->frambuf[12]  = 0x00;
	    			LCM->frambuf[13]  = 0x0E;
	    		break ;
	    	
	    	case 8:
	    			LCM->frambuf[12]  = 0x0F;
	    			LCM->frambuf[13]  = 0x0E;
	    		break ;
	    	
	    	case 9:
	    			LCM->frambuf[12]  = 0x0C;
	    			LCM->frambuf[13]  = 0x0E;
	    		break ;
		
	}
	
}
	
void point()
{
		LCM->frambuf[3]  |= 0x01;
}

/**
 * @brief       main function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
int main( void )
{
	unsigned char i  = 0;
	
	LCM_Config();
	
	while(1)
	{
	    /* set LCM display value */
	    Dig1(i);
	    Dig2(i);
	    point();
	    Dig3(i);
	    Dig4(i);
	    delay(500);
	    	
			 i++;
	     if(i >=10) i =0;
	}
}

